package com.sword.controller.stock;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.stock.product.Product;
import com.sword.pojo.stock.product.Productspec;
import com.sword.service.stock.product.ProductService;
import com.sword.service.stock.product.ProductSpecService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stock")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductSpecService productSpecService;
    /**
     * 商品总分类，细分类，计量单位分类渲染
     */
    @RequestMapping("/product")
    public String product() { return "views/stock/product"; }

    @RequestMapping("/productspec")
    public String productSpec() { return "views/stock/productspec"; }

    /**
     * 查询商品总分类列表功能
     */
    @RequestMapping("/queryProduct")
    @ResponseBody
    public PageResult queryProduct(@RequestBody QueryPageBean queryPageBean) {
        try {
            return productService.queryProduct(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_PRODOCT_FAIL, null, null);
        }
    }
    /**
     * 添加商品总分类功能
     */

    @ResponseBody
    @RequestMapping("/addProduct")
    public Result addProduct(@RequestBody Product product) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            product.setEmployeeid(userId);

            // 判断是否添加成功
            Integer flag = productService.addProduct(product);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_PRODOCT_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_PRODOCT_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_PRODOCT_SUCCESS, null);
    }

    @ResponseBody
    @RequestMapping("editProduct")
    public Result editProduct(@RequestBody Product product) {
        try {
            Integer flag = productService.editProduct(product);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_PRODOCT_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_PRODOCT_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_PRODOCT_SUCCESS, null);
    }

    /**
     * 删除商品总分类
     */

    @ResponseBody
    @RequestMapping("deleteProduct")
    public Result deleteProduct(Integer id) {
        try {
            Integer flag = productService.deleteProduct(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_PRODOCT_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_PRODOCT_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_PRODOCT_SUCCESS, null);
    }
    /**
     * 查询商品规格列表功能
     */
    @RequestMapping("/queryProductSpec")
    @ResponseBody
    public PageResult queryProductSpec(@RequestBody QueryPageBean queryPageBean) {
        try {
            return productSpecService.queryProductSpec(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_PRODOCTSPEC_FAIL, null, null);
        }
    }

    /**
     * 添加商品规格功能
     */
    @ResponseBody
    @RequestMapping("addProductSpec")
    public Result addProductSpec(@RequestBody Productspec productspec) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            productspec.setEmployeeid(userId);

            // 判断是否添加成功
            Integer flag = productSpecService.addProductSpec(productspec);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_PRODOCTSPEC_FAIL, null);
            }

        } catch (Exception e) {
            return new Result(500, MessageConstant.ADD_PRODOCTSPEC_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_PRODOCTSPEC_SUCCESS, null);
    }
    /**
     * 编辑商品规格功能
     */
    @ResponseBody
    @RequestMapping("editProductSpec")
    public Result editProductSpec(@RequestBody Productspec productspec) {
        try {
            Integer flag = productSpecService.editProductSpec(productspec);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_PRODOCTSPEC_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_PRODOCTSPEC_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_PRODOCTSPEC_SUCCESS, null);
    }
    /**
     * 删除商品规格功能
     */
    @ResponseBody
    @RequestMapping("deleteProductSpec")
    public Result deleteProductSpec(Integer id) {
        try {
            Integer flag = productSpecService.deleteProductSpec(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_PRODOCTSPEC_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_PRODOCTSPEC_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_PRODOCTSPEC_SUCCESS, null);
    }

    //  @RequestMapping("/stock/Productform")
    // public String productform() { return "views/stock/Productform"; }

    // @RequestMapping("/stock/ProductSpecform")
    // public String productspecform() { return "views/stock/ProductSpecform"; }
    /**
     * 查询所有数据用于可视化
     */
    @ResponseBody
    @RequestMapping("/getproduct")
    public Map getproduct() {
        List<Product> employees = productService.queryAll();
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data",employees);
        return map;
    }
}
